package leetCode.secondWeek;

import java.util.LinkedList;
import java.util.List;

public class Code_60 {
    public int count = 0;
    public String res = "";
    public int determination;

    public String getPermutation(int n, int k) {
        determination = k;
        if (n == 0 | n == 1) {
            return n == 0 ? "" : "1";
        }
        List<Integer> Copy = new LinkedList<>();
        for (int i = 1; i <= n; i++) {
            Copy.add(i);
        }
        /**
         * 1-剩余的字母
         * 2-已创建的字符串
         */
        bfs(Copy, "");
        System.out.println(res);
        return res;
    }

    public void bfs(List<Integer> leave, String tmp) {
        if (count >= determination) {
            return;
        }
        if (leave.isEmpty()) {
            count++;
            if (count == determination) {
                res = tmp;
            }
        }
        for (int i = 0; i < leave.size(); i++) {
            Integer tmpChar = leave.get(i);
            leave.remove(i);
            bfs(leave, tmp + tmpChar);
            leave.add(i, tmpChar);
        }
    }
}
